package com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest;

import android.content.Context;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.mlz.mlzmall.bean.UserBean;
import com.mlz.mlzmall.ui.activity.act.personal.PersonalActivity;
import com.mlz.mlzmall.ui.activity.frag.main.HomeFragment;
import com.mlz.mlzmall.ui.activity.frag.main.ShoppingCartFragment;
import com.mlz.mlzmall.ui.model.UserInfo;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.URL;
import com.mlz.mlzmall.ui.utils.util.ACache;
import com.mlz.mlzmall.ui.utils.util.SharedUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @description 网络请求
 * Created by 安帅 on 2016/5/23 15:14.
 */
public class RequestUtils {
    public static final String TGA = "RequestUtils";

    //登录请求
    public static JsonObjectRequest getMLZLoginRequest(final Context context, final String loginName, final String password, String deviceId, Response.Listener<JSONObject> listener) {
        JSONObject json = new JSONObject();
        try {
            json.put("telephone", loginName);
            json.put("password", password);
            json.put("deviceId", deviceId);
            json.put("vcode", "");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL.CUSTOMER.LOGIN, json, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ProgessTools.closeProgressDialog();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                headers.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + String.valueOf(SharedUtils.get(context, ConfigInfo.SESSIONKEY, "")));
                headers.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                Log.i(TGA, "登录的完整头部：" + String.valueOf(headers));
                return headers;
            }
        };
        Log.i(TGA, "RequestUtils登录" + ConfigInfo.COOKIE_KEY + String.valueOf(SharedUtils.get(context, ConfigInfo.SESSIONKEY, "")));
        return request;
    }

    /**
     * 登出
     *
     * @param listener
     * @return
     */
    public static StringRequest getMLZLoginOut(final Context context, Response.Listener<String> listener) {
        StringRequest request = new StringRequest(Request.Method.GET, URL.CUSTOMER.LOGOUT, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                headers.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + String.valueOf(SharedUtils.get(context, ConfigInfo.SESSIONKEY, "")));
                headers.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return headers;
            }

        };
//        Log.i(TGA, "获取信息"+ConfigInfo.COOKIE_KEY+String.valueOf(SharedUtils.get(context,ConfigInfo.SESSIONKEY,"")));
        return request;
    }

    /**
     * 获取用户信息（get）
     *
     * @param listener
     * @return
     */

    public static StringRequest getMLZRequestUserInfo(final Context context, Response.Listener<String> listener) {
        StringRequest request = new StringRequest(Request.Method.GET, URL.CUSTOMER.INFO, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                headers.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + ACache.get(context).getAsString("sessionKeyLogin"));
                headers.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                Log.i(TGA, "完整头部：" + String.valueOf(headers));
                return headers;
            }
        };
        Log.d(TGA, "请求用户信息时的session：" + ConfigInfo.COOKIE_KEY + ACache.get(context).getAsString("sessionKeyLogin"));
        Log.d(TGA, "请求用户信息时的URL：" + URL.CUSTOMER.INFO);
        return request;
    }

    //
    public static UserBean userInfOnResponse(String s) {
        MyCenterUtils.LogText("用户解析数据" + s);

        UserBean user = new UserBean();
        try {
            Gson gson = new Gson();
            UserInfo userInfo = gson.fromJson(s, UserInfo.class);
            int status = userInfo.getStatus();
            if (0 == status) {
                String fullname = userInfo.getData().getFullname();
                int customerId = userInfo.getData().getCustomerId();
                int addressId = userInfo.getData().getAddressId();
                int ageGroupTagId = userInfo.getData().getAgeGroupTagId();
                String ageGroupTagName = userInfo.getData().getAgeGroupTagName();
                int characterTagId = userInfo.getData().getCharacterTagId();
                String characterTagName = userInfo.getData().getCharacterTagName();
                int dressStyleTagId = userInfo.getData().getDressStyleTagId();
                String dressStyleTagName = userInfo.getData().getDressStyleTagName();
                int customerGroupName = userInfo.getData().getCustomerGroupName();
                String portrait = userInfo.getData().getPortrait();
                int gender = userInfo.getData().getGender();
                String telephone = userInfo.getData().getTelephone();

                user.setUserName(fullname);
                user.setCustomerId(customerId);
                user.setAddressId(addressId);
                user.setHeadersImage(portrait);
                user.setAccout(telephone);
                user.setAgeGroupTagId(ageGroupTagId);
                user.setUserAge(ageGroupTagName);
                user.setCharacterTagId(characterTagId);
                user.setUserCharacter(characterTagName);
                user.setDressStyleTagId(dressStyleTagId);
                user.setUserSex(gender);
                user.setUserStyle(dressStyleTagName);
                user.setCustomerGroupName(customerGroupName);


                //    MyCenterUtils.LogText("成功结束");
            } else {
                //  MyCenterUtils.ToastText("系统出错，操作失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return user;
    }


    //自动登录请求
    public static JsonObjectRequest getMLZAutomaticLoginRequest(final Context context, final String loginName, final String randomKey, String deviceId, Response.Listener<JSONObject> listener) {
        JSONObject json = new JSONObject();
        try {
            json.put("telephone", loginName);
            json.put("deviceId", deviceId);
            json.put("randomKey", randomKey);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL.CUSTOMER.LOGIN, json, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ProgessTools.closeProgressDialog();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                headers.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + String.valueOf(SharedUtils.get(context, ConfigInfo.SESSIONKEY, "")));
                headers.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return headers;
            }
        };
        return request;
    }

    //注册请求
    public static JsonObjectRequest getMLZRegistRequest(final Context context, final String phoneNum, final String code, final String pwd, final String confirmPassword, Response.Listener<JSONObject> listener) {
        JSONObject json = new JSONObject();
        try {
            json.put("telephone", phoneNum);
            json.put("password", pwd);
            json.put("confirmPassword", confirmPassword);
            json.put("smsCode", code);
            json.put("gender", "0");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL.CUSTOMER.REGISTER, json, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                headers.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + ACache.get(context).getAsString("sessionKeyTest"));
                headers.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return headers;
            }
        };
        Log.d(TGA, "请求注册时的session：" + ConfigInfo.COOKIE_KEY + ACache.get(context).getAsString("sessionKeyTest"));
        return request;
    }

    //请求验证码请求
    public static JsonObjectRequest getMLZRequestCode(final Context context, final String phoneNum, String type, Response.Listener<JSONObject> listener) {
        JSONObject json = new JSONObject();
        try {
            json.put("vcode", "");
            json.put("telephone", phoneNum);
            json.put("type", type);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL.CUSTOMER.SEND_SMS, json, listener,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                headers.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + ACache.get(context).getAsString("sessionKeyTest"));
                headers.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return headers;
            }
        };
        Log.d(TGA, "请求验证码时的session：" + ConfigInfo.COOKIE_KEY + ACache.get(context).getAsString("sessionKeyTest"));
        Log.d(TGA, "请求验证码时的url：" + URL.CUSTOMER.SEND_SMS);
        return request;
    }

    //验证注册
    public static JsonObjectRequest getMLZRegistCode(final Context context, final String phoneNum, final String code, Response.Listener<JSONObject> listener) {
        JSONObject json = new JSONObject();
        try {
            json.put("telephone", phoneNum);
            json.put("smsCode", code);
            Log.i(TGA,"后台code:"+code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL.CUSTOMER.VERIFY_REGISTER, json, listener
                , new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                headers.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + ACache.get(context).getAsString("sessionKeyTest"));
                headers.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return headers;
            }
        };
        Log.d(TGA, "请求验证注册时的session：" + ConfigInfo.COOKIE_KEY + ACache.get(context).getAsString("sessionKeyTest"));
        Log.d(TGA, "请求验证码时的url：" +  URL.CUSTOMER.VERIFY_REGISTER);
        return request;
    }

    // 找回密码 验证注册
    public static JsonObjectRequest getMLZForgetPwd(final Context context, final String phoneNum, final String code, Response.Listener<JSONObject> listener) {
        JSONObject json = new JSONObject();
        try {
            json.put("telephone", phoneNum);
            json.put("smsCode", code);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL.CUSTOMER.VERIFY_RESET_PWD, json, listener
                , new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                headers.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + ACache.get(context).getAsString("sessionKeyTest"));
                headers.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return headers;
            }
        };
        return request;
    }

    //找回密码 重设密码
    public static JsonObjectRequest getMLZChangePwd(final Context context, final String pwd, final String confirmPassword, Response.Listener<JSONObject> listener) {
        JSONObject json = new JSONObject();
        try {
            json.put("password", pwd);
            json.put("confirmPassword", confirmPassword);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL.CUSTOMER.RESET_PWD, json, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                headers.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + ACache.get(context).getAsString("sessionKeyTest"));
                headers.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return headers;
            }
        };
        return request;
    }

    //获取session
    public static StringRequest getSession(final Context context, Response.Listener<String> listener) {

        StringRequest request = new StringRequest(Request.Method.GET, URL.SESSION.GET, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> heads = new HashMap<>();
                heads.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                heads.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return heads;
            }
        };
        return request;
    }

    public static JsonObjectRequest POSTWarehouse(final Context mContext, String warehouse, int addressId, Response.Listener<JSONObject> listener) {
        JSONObject json = new JSONObject();
        try {
            json.put("warehouse", warehouse);
            json.put("addressId", addressId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL.CART.SET_ADDRESS, json, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                headers.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + ACache.get(mContext).getAsString("sessionKeyLogin"));
                headers.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return headers;
            }
        };
        return request;
    }

    public static JsonObjectRequest PostHeadData(final Context mContext, String head, String name, Response.Listener<JSONObject> listener) {
        JSONObject json = new JSONObject();
        try {
            json.put("data", head);
            json.put("fileName", name);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL.CUSTOMER.EDIT_PHOTO, json, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                headers.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + ACache.get(mContext).getAsString("sessionKeyLogin"));
                headers.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return headers;
            }
        };
        return request;
    }
}
